//
//  SinaNoLoginView.swift
//  MySinaWeiBoSwiftProject
//
//  Created by apple on 2016/11/13.
//  Copyright © 2016年 WangChen. All rights reserved.
//

import UIKit
import SnapKit

@objc protocol VisitorViewBtnClickProtocol: NSObjectProtocol {
    
    @objc optional func loginBtnDidClick()
    func registerBtnDidClick()
    
}


class SinaNoLoginView: UIView {

    weak var delegate : VisitorViewBtnClickProtocol?
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        
    }
    
    
    // MARK:
    // MARK: - 给外界设置访客界面的属性接口
    func updateInfo(title: String,imageName: String,isHome: Bool){
        
        titleLbl.text = title
        cycleView.image = UIImage(named: imageName)
        
        if isHome {
            
            let anim = CABasicAnimation(keyPath: "transform.rotation")
            anim.duration = 20
            anim.repeatCount = MAXFLOAT
            anim.toValue = M_PI * 2
            
            anim.isRemovedOnCompletion = false
            
            cycleView.layer.add(anim, forKey: nil)
            
        }else{
            
            houseIv.isHidden = true
            backView.isHidden = true
            
        }
        
    }
    
    
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func clickLoginBtn() {
        delegate?.loginBtnDidClick!()
    }
    
    func clickRegistBtn() {
        delegate?.registerBtnDidClick()
    }
    
    
    
    // MARK:
    // MARK: - 布置界面
    func setupUI() {
        
        loginBtn.addTarget(self, action: #selector(clickLoginBtn), for: .touchUpInside)
        registerBtn.addTarget(self, action: #selector(clickRegistBtn), for: .touchUpInside)
        
        addSubview(cycleView)
        addSubview(backView)
        addSubview(houseIv)
        addSubview(titleLbl)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        
        //设置约束
        cycleView.snp.makeConstraints { (make) in
            
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-70)
            
        }
        
        backView.snp.makeConstraints { (make) in
            
            make.center.equalTo(cycleView)
            
        }
        
        houseIv.snp.makeConstraints { (make) in
            
            make.center.equalTo(cycleView)
            
        }
        
        titleLbl.snp.makeConstraints { (make) in
            
            make.top.equalTo(cycleView.snp.bottom).offset(20)
            make.centerX.equalTo(cycleView)
            
        }
        
        loginBtn.snp.makeConstraints { (make) in
            
            make.leading.equalTo(titleLbl)
            make.top.equalTo(titleLbl.snp.bottom).offset(20)
            make.width.equalTo(100)
            
        }
        
        registerBtn.snp.makeConstraints { (make) in
            
            make.trailing.equalTo(titleLbl)
            make.top.equalTo(titleLbl.snp.bottom).offset(20)
            make.width.equalTo(loginBtn)
            
        }
        
        backgroundColor = UIColor.init(white: 237/255.0, alpha: 1)
        
    }
    
    
    
    
    
    
    // MARK:
    // MARK: - 懒加载忽略文件
    
    lazy var cycleView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_smallicon"))
    
    lazy var backView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_mask_smallicon"))
    
    lazy var houseIv = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_house"))
    
    lazy var titleLbl: UILabel = {
        
        let lbl = UILabel(title: "点击回来,关注更多,哈哈哈哈哈哈哈哈哈哈哈哈吼吼😁哈", font: 20, color: #colorLiteral(red: 0.501960814, green: 0.501960814, blue: 0.501960814, alpha: 1))
        lbl.numberOfLines = 0
        lbl.textAlignment = .center
        lbl.preferredMaxLayoutWidth = 230
        
        return lbl
    }()
    
    lazy var loginBtn: UIButton = {
        
        let btn = UIButton(title: "登录", font: 15, color: #colorLiteral(red: 0.501960814, green: 0.501960814, blue: 0.501960814, alpha: 1))
        btn.setBackgroundImage(#imageLiteral(resourceName: "common_button_white"), for: .normal)
        return btn
        
    }()
    
    lazy var registerBtn: UIButton = {
        
        let btn = UIButton(title: "注册", font: 15, color: UIColor.orange)
        btn.setBackgroundImage(#imageLiteral(resourceName: "common_button_white"), for: .normal)
        return btn
    }()
    
    
}
